package com.gxuwz.medical.domain.institution;

import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.area.AreaTreeNode;
import com.gxuwz.medical.domain.institution.*;

public class InstitutionTree {
	
	private List<InstTreeNode> nodeList;
	
	
	public InstitutionTree(String id)throws Exception{
		nodeList = new ArrayList<InstTreeNode>();
		Institution inst = new Institution(id);
		List<Institution> instList = inst.children();
		for(Institution i : instList){
			nodeList.add(tree(i,true));
		}
	}
	
	
	public InstTreeNode tree(Institution inst,boolean recursive) throws Exception {
		try {

			InstTreeNode node = new InstTreeNode(inst);
			List<Institution> subInstList = inst.children();
			if (!subInstList.isEmpty()) {
				if (recursive) {// 递归查询子节点
					List<InstTreeNode> children = new ArrayList<InstTreeNode>();
					for (Institution i : subInstList) {
						InstTreeNode t = tree(i, true);
						children.add(t);
					}
					node.setChildren(children);
				}
			}
			return node;
		} catch (Exception e) {
			throw new Exception("recursive create the node of tree failed!", e);
		}
	}


}
